<?php

class Controller_Signin extends Controller_Website
{

    public function before() {
        parent::before();
        $this->section = 'signin';
        $this->template->activeCat = 'account';
        $this->addScript('/scripts/jquery.placeholder.min.js');
    }

    public function actionIndex()
    {
        $this->template->title = $this->settings['title'];
        $this->template->content = new View('signin/index');

        $form = new Form('signin');
        $form->text('name', 'Name')->rule('required')->rule('maxLength', 32);
        $form->password('password', 'Password')->rule('required')->rule('maxLength', 32);
        $form->checkbox('persistent', '', 'Remember me on this computer');
        $form->submit('submit', 'Login');


        if (Request::$method == 'POST') {
            if ($form->validate()) {
                $values = $form->getValues();
                if (Auth::getInstance()->authenticate($values['name'], $values['password'], $values['persistent'])) {

                    if(isset($_SESSION['identity'])){
                        if($_SESSION['identity']['role'] ==='admin' || $_SESSION['identity']['role'] ==='root'){
                            $this->response->redirect('/admin/');
                        }
                    }
                    $this->response->redirect('/account/');


                } else {
                    echo '<script type="text/javascript">alert("This user is not registered.")</script>';
                }
            }
        }
        $this->template->content->form = $form;
    }
}